//
//  ZQTopicPictureView.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/15.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQTopicPictureView.h"
#import "ZQTopic.h"
#import "UIImageView+Download.h"
#import "ZQSeeBigPictureViewController.h"

@interface ZQTopicPictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;
@property (weak, nonatomic) IBOutlet UIButton *bottomBtn;

@end

@implementation ZQTopicPictureView


- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingNone;
    
    self.imageView.userInteractionEnabled = YES;
    
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seeBigPicture)]];
    

}

- (void)seeBigPicture
{
    ZQSeeBigPictureViewController *seeBigPicture = [[ZQSeeBigPictureViewController alloc]init];
    seeBigPicture.topic = self.topic;
    [self.window.rootViewController presentViewController:seeBigPicture animated:YES completion:nil];

}

- (void)setTopic:(ZQTopic *)topic
{
    _topic = topic;
    
    [self.imageView zq_setImageOriginalImageURL:topic.image1 thumbnailImageURL:topic.image0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image == nil) return ; //图片为空没有下载下来 return
        
        if (!topic.isBigPicture) return; //图片不为超长就return
        
        //上下文的尺寸 , 出来的图形的尺寸
        CGFloat imageW = topic.minFrame.size.width;
        CGFloat imageH = topic.minFrame.size.height;
        
        //开启图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(imageW, imageH));
        
        //绘图
        [image drawInRect:CGRectMake(0, 0, imageW, imageW * topic.height / topic.width)];
        
        //取出图形
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        
        //关闭图形上下文
        UIGraphicsEndImageContext();
    }];
    
    
    self.gifImageView.hidden = !topic.is_gif;
    self.bottomBtn.hidden = !topic.isBigPicture;
    
    
    
    
}

@end
